home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / Embed / Sources / Command.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  16.8 KB  |  565 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Command.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Author:                M.Boetcher
  7. //
  8. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "Embed.hpp"
  13.  
  14. #ifndef COMMAND_H
  15. #include "Command.h"
  16. #endif
  17.  
  18. #ifndef SELECT_H
  19. #include "Select.h"
  20. #endif
  21.  
  22. #ifndef PROXY_H
  23. #include "Proxy.h"
  24. #endif
  25.  
  26. #ifndef PART_H
  27. #include "Part.h"
  28. #endif
  29.  
  30. #ifndef FRAME_H
  31. #include "Frame.h"
  32. #endif
  33.  
  34. #ifndef CONTENT_H
  35. #include "Content.h"
  36. #endif
  37.  
  38. // ----- Framework Includes -----
  39.  
  40. #ifndef FWPRESEN_H
  41. #include "FWPresen.h"
  42. #endif
  43.  
  44. // ----- OS Includes -----
  45.  
  46. #ifndef FWORDCOL_H
  47. #include "FWOrdCol.h"
  48. #endif
  49.  
  50. // ----- OpenDoc Includes -----
  51.  
  52. #ifndef SOM_Module_OpenDoc_Commands_defined
  53. #include <CmdDefs.xh>
  54. #endif
  55.  
  56. //========================================================================================
  57. // RunTime Info
  58. //========================================================================================
  59.  
  60. #ifdef FW_BUILD_MAC
  61. #pragma segment opfEmbed
  62. #endif
  63.  
  64. FW_DEFINE_AUTO(CEmbedEditCommand)
  65. FW_DEFINE_AUTO(CEmbedInsertCommand)
  66. FW_DEFINE_AUTO(CEmbedDropCommand)
  67. FW_DEFINE_AUTO(CEmbedDragCommand)
  68.  
  69. //========================================================================================
  70. // CEmbedEditCommand
  71. //========================================================================================
  72.  
  73. //----------------------------------------------------------------------------------------
  74. //    CEmbedEditCommand constructor
  75. //----------------------------------------------------------------------------------------
  76.  
  77. CEmbedEditCommand::CEmbedEditCommand(Environment *ev, 
  78.                                      ODCommandID commandID,
  79.                                      CEmbedContent* content, 
  80.                                      FW_CFrame* frame, 
  81.                                      FW_CSelection* selection,
  82.                                      FW_Boolean canUndo) :
  83.     FW_CClipboardCommand(ev, commandID, frame, canUndo),
  84.         fEmbedContent(content),
  85.         fPastedProxy(NULL),
  86.         fOldProxy(NULL)
  87. {
  88. FW_UNUSED(selection);
  89. }
  90.  
  91. //----------------------------------------------------------------------------------------
  92. //    CEmbedEditCommand destructor
  93. //----------------------------------------------------------------------------------------
  94.  
  95. CEmbedEditCommand::~CEmbedEditCommand()
  96. {
  97. }
  98.  
  99. //----------------------------------------------------------------------------------------
  100. //    CEmbedEditCommand::SaveUndoState
  101. //----------------------------------------------------------------------------------------
  102.  
  103. void CEmbedEditCommand::SaveUndoState(Environment* ev)    // Override
  104. {
  105.     ODCommandID commandID = GetCommandID(ev);
  106.     
  107.     if (commandID == kODCommandCut || commandID == kODCommandClear)
  108.         fPastedProxy = fEmbedContent->GetProxy();
  109.     else if (commandID == kODCommandPaste)
  110.         fOldProxy = fEmbedContent->GetProxy();
  111. }
  112.  
  113. //----------------------------------------------------------------------------------------
  114. //    CEmbedEditCommand::FreeUndoState
  115. //----------------------------------------------------------------------------------------
  116.  
  117. void CEmbedEditCommand::FreeUndoState(Environment* ev)    // Override
  118. {
  119.     ODCommandID commandID = GetCommandID(ev);
  120.     
  121.     if (commandID == kODCommandCut || commandID == kODCommandClear)
  122.     {
  123.         delete fPastedProxy;
  124.     }
  125.     else if (commandID == kODCommandPaste)
  126.     {
  127.         // Delete the original proxy
  128.         delete fOldProxy;
  129.     }
  130. }
  131.  
  132. //----------------------------------------------------------------------------------------
  133. //    CEmbedEditCommand::SaveRedoState
  134. //----------------------------------------------------------------------------------------
  135.  
  136. void CEmbedEditCommand::SaveRedoState(Environment *ev)    // Override
  137. {
  138.     if (GetCommandID(ev) == kODCommandPaste) 
  139.         fPastedProxy = fEmbedContent->GetProxy();
  140. }
  141.  
  142. //----------------------------------------------------------------------------------------
  143. //    CEmbedEditCommand::FreeRedoState
  144. //----------------------------------------------------------------------------------------
  145.  
  146. void CEmbedEditCommand::FreeRedoState(Environment* ev)    // Override
  147. {
  148.     if (GetCommandID(ev) == kODCommandPaste)
  149.     {
  150.         // Delete the pasted proxy
  151.         delete fPastedProxy;
  152.     }
  153. }
  154.  
  155. //----------------------------------------------------------------------------------------
  156. //    CEmbedEditCommand::UndoIt
  157. //----------------------------------------------------------------------------------------
  158.  
  159. void CEmbedEditCommand::UndoIt(Environment *ev)    // Override
  160. {
  161.     FW_CClipboardCommand::UndoIt(ev);
  162.  
  163.     switch (GetCommandID(ev))
  164.     {
  165.         case kODCommandCut:
  166.         case kODCommandClear:
  167.             this->RestorePart(ev);
  168.             break;
  169.  
  170.         case kODCommandPaste:
  171.             this->RemovePart(ev);
  172.             this->RestoreOldPart(ev);
  173.             break;
  174.     }    
  175. }
  176.  
  177. //----------------------------------------------------------------------------------------
  178. //    CEmbedEditCommand::RedoIt
  179. //----------------------------------------------------------------------------------------
  180.  
  181. void CEmbedEditCommand::RedoIt(Environment *ev)    // Override
  182. {
  183.     FW_CClipboardCommand::RedoIt(ev);
  184.  
  185.     switch (GetCommandID(ev))
  186.     {
  187.         case kODCommandCut:
  188.         case kODCommandClear:
  189.             this->RemovePart(ev);
  190.             break;
  191.  
  192.         case kODCommandPaste:
  193.             this->RemovePart(ev);
  194.             this->RestorePart(ev);
  195.             break;
  196.     }    
  197. }
  198.  
  199. //----------------------------------------------------------------------------------------
  200. //    CEmbedEditCommand::RestorePart
  201. //----------------------------------------------------------------------------------------
  202.  
  203. void CEmbedEditCommand::RestorePart(Environment *ev)    // Override
  204. {
  205.     fEmbedContent->SetProxy(fPastedProxy);
  206.     fPastedProxy->AttachEmbeddedFrames(ev);
  207.         
  208.     GetPresentation(ev)->Invalidate(ev);
  209. }
  210.  
  211. //----------------------------------------------------------------------------------------
  212. //    CEmbedEditCommand::RemovePart
  213. //----------------------------------------------------------------------------------------
  214.  
  215. void CEmbedEditCommand::RemovePart(Environment *ev)    // Override
  216. {
  217.     // Clear the selection, which includes detaching the proxy frames.
  218.     fSelection->ClearSelection(ev);
  219. }
  220.  
  221. //----------------------------------------------------------------------------------------
  222. //    CEmbedEditCommand::RestoreOldPart
  223. //----------------------------------------------------------------------------------------
  224.  
  225. void CEmbedEditCommand::RestoreOldPart(Environment* ev)
  226. {
  227.     // restore the previous part, if any
  228.     if (fOldProxy)
  229.     {
  230.         fEmbedContent->SetProxy(fOldProxy);
  231.         if (fOldProxy)
  232.             fOldProxy->AttachEmbeddedFrames(ev);
  233.  
  234.         // Force a redraw
  235.         GetPresentation(ev)->Invalidate(ev);
  236.     }
  237. }
  238.  
  239. //========================================================================================
  240. //    class CEmbedInsertCommand
  241. //========================================================================================
  242.  
  243. //----------------------------------------------------------------------------------------
  244. //    CEmbedInsertCommand constructor
  245. //----------------------------------------------------------------------------------------
  246.  
  247. CEmbedInsertCommand::CEmbedInsertCommand(Environment* ev,
  248.                                          FW_CEmbeddingFrame* frame,
  249.                                          const FW_PFileSpecification& fileSpec,
  250.                                          CEmbedContent* content)
  251. :    FW_CInsertCommand(ev, frame, fileSpec, FW_kCanUndo),
  252.     fPartContent(content),
  253.     fEmbedSelection(NULL),
  254.     fInsertedProxy(NULL),
  255.     fOldProxy(NULL)
  256. {
  257.     fEmbedSelection = (CEmbedSelection*) frame->GetPresentation(ev)->GetSelection(ev);
  258.     FW_END_CONSTRUCTOR
  259. }
  260.  
  261. //----------------------------------------------------------------------------------------
  262. //    CEmbedInsertCommand destructor
  263. //----------------------------------------------------------------------------------------
  264.  
  265. CEmbedInsertCommand::~CEmbedInsertCommand()
  266. {
  267. }
  268.  
  269. //---------------------------------------------------------------------------------------
  270. //    CEmbedInsertCommand::UndoIt
  271. //---------------------------------------------------------------------------------------
  272.  
  273. void CEmbedInsertCommand::UndoIt(Environment* ev)
  274. {
  275.     // remove the inserted part
  276.     fEmbedSelection->ClearSelection(ev);
  277.  
  278.     // restore the previous part, if any
  279.     fPartContent->SetProxy(fOldProxy);
  280.     if (fOldProxy)
  281.         fOldProxy->AttachEmbeddedFrames(ev);
  282.  
  283.     // Force a redraw
  284.     GetPresentation(ev)->Invalidate(ev);
  285. }
  286.  
  287. //---------------------------------------------------------------------------------------
  288. //    CEmbedInsertCommand::RedoIt
  289. //---------------------------------------------------------------------------------------
  290.  
  291. void CEmbedInsertCommand::RedoIt(Environment* ev)
  292. {
  293.     // remove the original embedded part
  294.     fEmbedSelection->ClearSelection(ev);
  295.  
  296.     // restore the inserted part
  297.     fPartContent->SetProxy(fInsertedProxy);
  298.     fInsertedProxy->AttachEmbeddedFrames(ev);
  299.  
  300.     // Force a redraw
  301.     GetPresentation(ev)->Invalidate(ev);
  302. }
  303.  
  304. //---------------------------------------------------------------------------------------
  305. //    CEmbedInsertCommand::FreeUndoState
  306. //---------------------------------------------------------------------------------------
  307. //    If the fOldProxy is not detached it means that the insert command failed
  308.  
  309. void CEmbedInsertCommand::FreeUndoState(Environment* ev)
  310. {
  311.     if (fOldProxy && fOldProxy->IsDetached(ev))
  312.     {
  313.         // Delete the original proxy
  314.         delete fOldProxy;
  315.         fOldProxy = NULL;
  316.     }
  317. }
  318.  
  319. //---------------------------------------------------------------------------------------
  320. //    CEmbedInsertCommand::FreeRedoState
  321. //---------------------------------------------------------------------------------------
  322.  
  323. void CEmbedInsertCommand::FreeRedoState(Environment* ev)
  324. {
  325. FW_UNUSED(ev);
  326.     // Delete the inserted proxy
  327.     delete fInsertedProxy;
  328. }
  329.  
  330. //---------------------------------------------------------------------------------------
  331. //    CEmbedInsertCommand::SaveUndoState
  332. //---------------------------------------------------------------------------------------
  333.  
  334. void CEmbedInsertCommand::SaveUndoState(Environment* ev)
  335. {
  336. FW_UNUSED(ev);
  337.     // Save the proxy for the part that's currently embedded
  338.     fOldProxy = fPartContent->GetProxy();
  339. }
  340.  
  341. //---------------------------------------------------------------------------------------
  342. //    CEmbedInsertCommand::SaveRedoState
  343. //---------------------------------------------------------------------------------------
  344.  
  345. void CEmbedInsertCommand::SaveRedoState(Environment* ev)
  346. {
  347. FW_UNUSED(ev);
  348.     // Save the proxy for the part that was just inserted
  349.     fInsertedProxy = fPartContent->GetProxy();
  350. }
  351.  
  352. //========================================================================================
  353. //    class CEmbedDropCommand
  354. //========================================================================================
  355.  
  356. //----------------------------------------------------------------------------------------
  357. //    CEmbedDropCommand constructor
  358. //----------------------------------------------------------------------------------------
  359.  
  360. CEmbedDropCommand::CEmbedDropCommand(Environment* ev,
  361.                                      CEmbedContent* content,
  362.                                      FW_CFrame* frame,
  363.                                      ODDragItemIterator* dropInfo, 
  364.                                      ODFacet* odFacet,
  365.                                      const FW_CPoint& dropPoint) : 
  366.     FW_CDropCommand(ev, frame, dropInfo, odFacet, dropPoint, FW_kCanUndo),
  367.     fEmbedContent(content),
  368.     fDroppedProxy(NULL),
  369.     fOldProxy(NULL)
  370. {
  371.     fEmbedSelection = (CEmbedSelection*) frame->GetPresentation(ev)->GetSelection(ev);
  372.     this->SetMenuStrings(ev, "Undo Drop", "Redo Drop");
  373.  
  374.     FW_END_CONSTRUCTOR
  375. }
  376.  
  377. //----------------------------------------------------------------------------------------
  378. //    CEmbedDropCommand destructor
  379. //----------------------------------------------------------------------------------------
  380.  
  381. CEmbedDropCommand::~CEmbedDropCommand()
  382. {
  383. }
  384.  
  385. //---------------------------------------------------------------------------------------
  386. //    CEmbedDropCommand::UndoIt
  387. //---------------------------------------------------------------------------------------
  388.  
  389. void CEmbedDropCommand::UndoIt(Environment* ev)
  390. {
  391.     FW_ASSERT(fDroppedProxy);
  392.  
  393.     // remove the dropped part
  394.     fEmbedSelection->ClearSelection(ev);
  395.  
  396.     // restore the previous part, if any
  397.     fEmbedContent->SetProxy(fOldProxy);
  398.     if (fOldProxy)
  399.         fOldProxy->AttachEmbeddedFrames(ev);
  400.  
  401.     // Force a redraw
  402.     GetPresentation(ev)->Invalidate(ev);
  403. }
  404.  
  405. //---------------------------------------------------------------------------------------
  406. //    CEmbedDropCommand::RedoIt
  407. //---------------------------------------------------------------------------------------
  408.  
  409. void CEmbedDropCommand::RedoIt(Environment* ev)
  410. {
  411.     FW_ASSERT(fDroppedProxy);
  412.  
  413.     // remove the original embedded part
  414.     fEmbedSelection->ClearSelection(ev);
  415.  
  416.     // restore the dropped part
  417.     fEmbedContent->SetProxy(fDroppedProxy);
  418.     fDroppedProxy->AttachEmbeddedFrames(ev);
  419.  
  420.     // Force a redraw
  421.     GetPresentation(ev)->Invalidate(ev);
  422. }
  423.  
  424. //----------------------------------------------------------------------------------------
  425. //    CEmbedDropCommand::SaveUndoState
  426. //----------------------------------------------------------------------------------------
  427.  
  428. void CEmbedDropCommand::SaveUndoState(Environment *ev)
  429. {
  430. FW_UNUSED(ev);
  431.     // Save the proxy for the part that's currently embedded
  432.     fOldProxy = fEmbedContent->GetProxy();
  433. }
  434.  
  435. //---------------------------------------------------------------------------------------
  436. //    CEmbedDropCommand::SaveRedoState
  437. //---------------------------------------------------------------------------------------
  438.  
  439. void CEmbedDropCommand::SaveRedoState(Environment *ev)
  440. {
  441. FW_UNUSED(ev);
  442.     // Save the proxy for the part that was just dropped
  443.     fDroppedProxy = fEmbedContent->GetProxy();
  444. }
  445.  
  446. //---------------------------------------------------------------------------------------
  447. //    CEmbedDropCommand::FreeUndoState
  448. //---------------------------------------------------------------------------------------
  449. //    If the fOldProxy is not detached it means that the drop command failed
  450.  
  451. void CEmbedDropCommand::FreeUndoState(Environment* ev)
  452. {
  453.     if (fOldProxy && fOldProxy->IsDetached(ev))
  454.     {
  455.         // Delete the original proxy
  456.         delete fOldProxy;
  457.         fOldProxy = NULL;
  458.     }
  459. }
  460.  
  461. //---------------------------------------------------------------------------------------
  462. //    CEmbedDropCommand::FreeRedoState
  463. //---------------------------------------------------------------------------------------
  464.  
  465. void CEmbedDropCommand::FreeRedoState(Environment* ev)
  466. {
  467. FW_UNUSED(ev);
  468.     // Delete the dropped proxy
  469.     delete fDroppedProxy;
  470. }
  471.  
  472. //----------------------------------------------------------------------------------------
  473. //    CEmbedDropCommand::DoDroppedInSameFrame
  474. //----------------------------------------------------------------------------------------
  475.  
  476. FW_Boolean CEmbedDropCommand::DoDroppedInSameFrame(Environment* ev, 
  477.                                               ODStorageUnit* dropSU, 
  478.                                               const FW_CPoint& mouseDownOffset, 
  479.                                               const FW_CPoint& dropPoint)
  480. {
  481.     FW_UNUSED(ev);
  482.     FW_UNUSED(dropSU);
  483.     FW_UNUSED(mouseDownOffset);
  484.     FW_UNUSED(dropPoint);
  485.     return FALSE;
  486. }
  487.  
  488. //========================================================================================
  489. //    class CEmbedDragCommand
  490. //========================================================================================
  491.  
  492. //----------------------------------------------------------------------------------------
  493. //    CEmbedDragCommand constructor
  494. //----------------------------------------------------------------------------------------
  495.  
  496. CEmbedDragCommand::CEmbedDragCommand(Environment* ev,
  497.                                     CEmbedContent* content,
  498.                                     FW_CFrame* frame,
  499.                                     CEmbedSelection* selection) : 
  500.     FW_CDragCommand(ev, frame, FW_kCanUndo),
  501.     fEmbedContent(content),
  502.     fEmbedSelection(selection),
  503.     fSavedProxy(NULL),
  504.     fDraggedProxy(NULL)
  505. {
  506.     fDraggedProxy = content->GetProxy();
  507. }
  508.  
  509. //----------------------------------------------------------------------------------------
  510. //    CEmbedDragCommand destructor
  511. //----------------------------------------------------------------------------------------
  512.  
  513. CEmbedDragCommand::~CEmbedDragCommand()
  514. {
  515. }
  516.  
  517. //---------------------------------------------------------------------------------------
  518. //    CEmbedDragCommand::UndoIt
  519. //---------------------------------------------------------------------------------------
  520.  
  521. void CEmbedDragCommand::UndoIt(Environment* ev)
  522. {
  523.     FW_ASSERT(fSavedProxy);
  524.  
  525.     // Restore saved proxy to the table
  526.     fSavedProxy->AttachEmbeddedFrames(ev);
  527.     fEmbedContent->SetProxy(fSavedProxy);
  528. }
  529.  
  530. //---------------------------------------------------------------------------------------
  531. //    CEmbedDragCommand::RedoIt
  532. //---------------------------------------------------------------------------------------
  533.  
  534. void CEmbedDragCommand::RedoIt(Environment* ev)
  535. {
  536.     fEmbedSelection->ClearSelection(ev);
  537. }
  538.  
  539. //---------------------------------------------------------------------------------------
  540. //    CEmbedDragCommand::SaveUndoState
  541. //---------------------------------------------------------------------------------------
  542.  
  543. void CEmbedDragCommand::SaveUndoState(Environment* ev)
  544. {
  545. FW_UNUSED(ev);
  546.  
  547.     // Save the proxy for the part that was dragged
  548.     fSavedProxy = fDraggedProxy;
  549. }
  550.  
  551. //---------------------------------------------------------------------------------------
  552. //    CEmbedDragCommand::FreeUndoState
  553. //---------------------------------------------------------------------------------------
  554.  
  555. void CEmbedDragCommand::FreeUndoState(Environment* ev)
  556. {
  557. FW_UNUSED(ev);
  558.  
  559. //    fSavedProxy = NULL;
  560.     FW_ASSERT(fSavedProxy);
  561.     delete fSavedProxy;
  562. }
  563.  
  564.  
  565.